package com.lu.common.test.oss

import android.app.Application
import android.os.Environment
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import com.lu.oss.OSSClientBuilder
import com.lu.oss.api.IOssObjectListener
import com.lu.oss.api.IOssService
import javax.inject.Inject

/**
 *author: luqihua
 *date:2023/11/20
 *description:
 **/

@HiltViewModel
class OssTestModel @Inject constructor(
        private val mApplication: Application
) : AndroidViewModel(mApplication) {
    companion object {
        private const val TAG = "OssTestModel"
        private const val testObjectKey =
                        "OTA_RG-IIB-SCOS2.3(0)B4_Release(20211122122154).zip"
//            "chayi4k.mp4"

    }

    private val mOssService by lazy { getOssService() }
    val progressState = MutableStateFlow(0f)

    init {

    }

    fun startDownload() {
        progressState.value = 0f
        val downloadDir = mApplication.getExternalFilesDir("oss")?.absolutePath
            ?: Environment.getExternalStorageDirectory().absolutePath

        val downloadFilePath = if (testObjectKey.contains("/")) {
            downloadDir + "/" + testObjectKey.substring(testObjectKey.lastIndexOf("/"))
        } else {
            "$downloadDir/$testObjectKey"
        }
        Log.d(TAG, "startDownload : downloadPath=$downloadFilePath")
        viewModelScope.launch(Dispatchers.Default) {
            mOssService.downloadFile(
                    testObjectKey,
                    downloadFilePath,
                    object : IOssObjectListener {
                        override fun onProgress(progress: Int) {
                            Log.d(TAG, "onProgress : $progress")
                            progressState.value = progress / 100f
                        }

                        override fun onSuccess() {
                            Log.d(TAG, "onSuccess : $downloadFilePath")
                        }

                        override fun onFailure(throwable: Throwable?) {
                            Log.e(TAG, "onFailure: ", throwable)
                        }
                    })
        }
    }

    fun stopDownload() {
        mOssService.stopTask(testObjectKey)
    }

    override fun onCleared() {
        mOssService.release()
    }

    private fun getOssService(): IOssService {
        val buckName = "lu-test-bucket"
        val accessKeyId = "LTAI5t7AAG1mYJKY3XEUtJfK"
        val accessKeySecret = "efpFYd5kdftJma8TWH5TfyfoLnf10D"
        val accessEndpoint = "https://oss-cn-hangzhou.aliyuncs.com"
        val ossClientBuilder = OSSClientBuilder(
                getApplication(),
                false,
                buckName,
                accessKeyId, accessKeySecret, accessEndpoint
        )
        return ossClientBuilder.build()
    }
}